package com.ldc.framework.activity

import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.ldc.commonlibs.conf.RouteConf
import com.ldc.commonres.common.VBActivity
import com.ldc.framework.R
import com.ldc.framework.databinding.FrameworkActivityFrameworkBinding
import com.ldc.framework.fragment.FindFragment
import com.ldc.framework.fragment.HomeFragment
import com.ldc.framework.fragment.ProfileFragment
import com.ldc.framework.fragment.QAFragment


@Route(path = RouteConf.FrameworkRoute.startEnter)
class FrameworkActivity : VBActivity<FrameworkActivityFrameworkBinding>() {
    private var currFragment: Fragment = Fragment()
    private var homeFragment: HomeFragment? = null
    private var qaFragment: QAFragment? = null
    private var findFragment: FindFragment? = null
    private var profileFragment: ProfileFragment? = null

    companion object {
        const val HOME_FRAGMENT = "home"
        const val QA_FRAGMENT = "qa"
        const val FIND_FRAGMENT = "find"
        const val PROFILE_FRAGMENT = "profile"
    }

    override fun main() {
        // 默认是首页
        changeFragment(getFragment(HOME_FRAGMENT))
        changePageTab()
        //
        viewBind?.let {
            it.flHome.setOnClickListener {
                changeFragment(getFragment(HOME_FRAGMENT))
                changePageTab(0)
            }
            it.flQa.setOnClickListener {
                changeFragment(getFragment(QA_FRAGMENT))
                changePageTab(1)
            }
            it.flFind.setOnClickListener {
                changeFragment(getFragment(FIND_FRAGMENT))
                changePageTab(2)
            }
            it.flProfile.setOnClickListener {
                changeFragment(getFragment(PROFILE_FRAGMENT))
                changePageTab(3)
            }
        }

    }

    override fun createViewBinding(): FrameworkActivityFrameworkBinding =
        FrameworkActivityFrameworkBinding.inflate(layoutInflater)


    // 切换
    private fun changeFragment(fragment: Fragment) {
        if (currFragment != fragment) {
            val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
            if (fragment.isAdded) {
                fragmentTransaction.show(fragment)
                    .hide(currFragment)
                    .commit()
            } else {
                fragmentTransaction.add(R.id.fl_container, fragment)
                    .show(fragment)
                    .hide(currFragment)
                    .commit()
            }
            currFragment = fragment
        }
    }

    // 获取试图
    private fun getFragment(tag: String = HOME_FRAGMENT): Fragment {
        when (tag) {
            HOME_FRAGMENT -> {
                homeFragment = homeFragment ?: HomeFragment.newInstance()
                return homeFragment!!
            }

            QA_FRAGMENT -> {
                qaFragment = qaFragment ?: QAFragment.newInstance()
                return qaFragment!!
            }
            FIND_FRAGMENT -> {
                findFragment = findFragment ?: FindFragment.newInstance()
                return findFragment!!
            }
            PROFILE_FRAGMENT -> {
                profileFragment = profileFragment ?: ProfileFragment.newInstance()
                return profileFragment!!
            }
            else -> {
                homeFragment = homeFragment ?: HomeFragment.newInstance()
                return homeFragment!!
            }

        }
    }


    private fun changePageTab(pos: Int = 0) {
        viewBind?.also { v ->
            v.tvHome.setPadding(0, 0, 0, if (0 == pos) 5 else 0)
            v.tvHome.setTextColor(if (0 == pos) Color.parseColor("#ff824c") else Color.parseColor("#8a8a8a"))

            v.tvQa.setPadding(0, 0, 0, if (1 == pos) 5 else 0)
            v.tvQa.setTextColor(if (1 == pos) Color.parseColor("#ff824c") else Color.parseColor("#8a8a8a"))

            v.tvFind.setPadding(0, 0, 0, if (2 == pos) 5 else 0)
            v.tvFind.setTextColor(if (2 == pos) Color.parseColor("#ff824c") else Color.parseColor("#8a8a8a"))

            v.tvProfile.setPadding(0, 0, 0, if (3 == pos) 5 else 0)
            v.tvProfile.setTextColor(
                if (3 == pos) Color.parseColor("#ff824c") else Color.parseColor(
                    "#8a8a8a"
                )
            )
            v.ivHome.setBackgroundResource(if (0 == pos) com.ldc.commonres.R.drawable.res_icon_home_pre else com.ldc.commonres.R.drawable.res_icon_home,)
            v.ivQa.setBackgroundResource(if (1 == pos) com.ldc.commonres.R.drawable.res_icon_qa_pre else com.ldc.commonres.R.drawable.res_icon_qa,)
            v.ivFind.setBackgroundResource(if (2 == pos) com.ldc.commonres.R.drawable.res_icon_find_pre else com.ldc.commonres.R.drawable.res_icon_find,)
            v.ivProfile.setBackgroundResource(if (3 == pos) com.ldc.commonres.R.drawable.res_icon_profile_pre else com.ldc.commonres.R.drawable.res_icon_profile)

        }
    }


}